package com.hj.dapp.common.resp;

import com.hj.dapp.config.exp.BusinessErrorEnum;
import com.hj.dapp.config.exp.BusinessErrorException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2023/12/29
 */
@Data
@Schema(description = "响应返回数据对象")
public class RestResp<T> {
    @Schema(title = "data", description = "响应数据", accessMode = Schema.AccessMode.READ_ONLY)
    private T data;
    @Schema(
            title = "code",
            description = "响应码",
            format = "int32",
           required = true
    )
    private Integer code;
    @Schema(
            title = "msg",
            description = "响应信息",
            accessMode = Schema.AccessMode.READ_ONLY,
            example = "成功或失败",
           required = true
    )
    private String msg;

    public RestResp() {
    }

    public RestResp(T data, Integer code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> RestResp<T> ok(T data) {
        return new RestResp<T>(data, HttpServletResponse.SC_OK,"OK");
    }
    public static RestResp<?> ok() {
        return new RestResp<>(null, HttpServletResponse.SC_OK,"OK");
    }
    public static RestResp<?> error() {
        return new RestResp<>(null, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"error");
    }
    public static RestResp<?> error(BusinessErrorException businessErrorException) {
        return new RestResp<>(null, businessErrorException.getCode(),businessErrorException.getMessage());
    }
    public static RestResp<?> error(BusinessErrorEnum businessErrorEnum) {
        return new RestResp<>(null, businessErrorEnum.getCode(),businessErrorEnum.getMsg());
    }
    public static RestResp<?> error(String msg) {
        return new RestResp<>(null, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg);
    }
}
